Demos:
Corrections:
Most of my life, my Dad has told me, "Son, you are born to be a helper." He usually preceded that remark with, "Mistakes will happen, but must you give them so much help?"
Many years ago, a friend of mine asked me to help him write a program to record data on the winning football teams every Sunday. I know absolutely nothing about sports, so all I had to do was record the data he considered important, evaluate it according to his requirements, and display it so he could make his selections for the coming week. (Sports betting is a big business here in Nevada.) My friend has won quite a bit of money over the past twenty-five years, and I still don't know anything about sports. I've never watched any type of game and don't care to.
At the time I wrote his program, I didn't know much about data files either, and his computer was a Timex Sinclair ZX-80, with only 16K of RAM, so I collected all the data to be processed in a single string for each game. The data was saved to an audio cassette and more data appended each week after the game results were known. Processing was slow, but what can you expect from a $49.95 Timex computer?
Data base management is a highly specialized field of programming, and the study needed is intensive. The home hobbyist/programmer can still make good use of data files with only the native commands in Liberty Basic.
Data strings are not the best way to store your data, but they are useful. The data can be easily sorted and stored and all data in that string will remain together. I refer to these as Six-Pak Data Strings. Six-Paks, afterall, are portable multiple beverage storage containers to keep similar items together in the same place.
Below is a sample data file for the Fancy Coin Collector's Shop, and a program to input, sort, and display the data using different criteria. Since I know nothing about coin collections, the only purpose is to show methods to maintain a data file and sort according to different choices. You can use similar methods for a video tape collection, team or league scores, class averages or activities of a small business.
The Data File
The data for this example is a text file. Individual items within the data file are called records and, in this file, each record contains six data fields, your six-pak of data. This data can be copied and saved and will allow you to play with the program without creating your own data file at this time.
12345678911234567892123456789312345678941234567895123456789
10 1947 Denver EXFINE C10
50 1863 New Orleans FINE A05 Civil War
25 2003 Philadelphia UN B03 MA
25 2003 Philadelphia UN B04 VT
01 1906 Philadelphia UN E09
05 1950 Denver VFINE C06
01 1912 West Point EXFINE E08
25 1917 Denver VFINE B06
10 1944 Philadelphia VFINE G02
100 1918 Carson City EXFINE B05
50 1965 West Point UN A07 JFK
25 2002 West Point UN B03 CT
25 2002 West Point UN B05 DE
100 1898 San Francisco VFINE G02
10 1956 Philadelphia UN F04
100 1921 San Francisco UN B05 Liberty
05 1902 West Point VFINE B08 Indian
100 1864 Charlotte FAIR A02 Civil War
100 1895 Carson City VFINE A03
05 1898 Philadelphia FINE G08
Create a sub-folder within your LB folder for My Coin Collection. Copy and save the data above as coins.xyz within that folder. Be sure you have copied the first string, which is called an index string.
Hey! I thought this was supposed to be a text file? Of course it is. The file extension is used only to associate a data file with an application to process the data. All data consists of 0's and 1's, but individual applications know how to process the data according to the file extension. Software developers often use file extensions to make a file proprietary to their application. You can use any file extension which suits you, as long as it is not already associated with another application used by your computer.
The Aplication
After saving the data file, copy and paste the myCoins.bas code into your folder.
'My Coins Using a Data File
'Written by Welo, 012405
OPEN "coins.xyz" for INPUT as #1
WHILE EOF(#1)=0
LINE INPUT #1, coins$
cntr=cntr+1 'Count the records in our data file.
WEND
CLOSE #1
DIM coins$(cntr)
cntr=0 'Reset cntr to zero
OPEN "coins.xyz" for INPUT as #1 'This time, load the array.
WHILE 1=1 'Endless loop. (Thanks to Brad for this tip! ;-)
LINE INPUT #1, coins$(cntr) 'The index$ will be myCoins$(0)
IF EOF(#1)<>0 THEN EXIT WHILE 'Check for EOF; exit if done.
cntr=cntr+1
WEND
CLOSE #1 'Close the file upon exiting the loop.
[choose]
CLS 'Clears the screen.
TIMER 0 'Set timer to zero.
PRINT "Choose your method to sort the coin collection:"
PRINT tab(5);"Sort by date on coin = 1"
PRINT tab(5);"Sort by coin denomination = 2"
PRINT tab(5);"Sort by mint location = 3"
PRINT tab(5);"Sort by remark = 4"
PRINT
INPUT "Your choice? "; selection
SELECT CASE selection
CASE 1 'Sort by coin date.
FOR i = 1 TO cntr 'Do not include the index string (coins$(0))
FOR k = 1 TO (cntr - 1)
IF VAL(MID$(coins$(k),7,4))>VAL(MID$(coins$(k+1),7,4)) THEN
hold$ = coins$(k) 'These three lines
coins$(k) = coins$(k + 1) 'swap strings if value
coins$(k + 1) = hold$ 'is out of order.
END IF
NEXT k
NEXT i
GOTO [display]
CASE 2 'Sort by coin denomination.
FOR i = 1 TO cntr 'Do not include the index string (coins$(0))
FOR k = 1 TO (cntr - 1)
IF VAL(MID$(coins$(k),1,5))>VAL(MID$(coins$(k+1),1,5)) THEN
hold$ = coins$(k) 'These three lines
coins$(k) = coins$(k + 1) 'swap strings if value
coins$(k + 1) = hold$ 'is out of order.
END IF
NEXT k
NEXT i
GOTO [display]
CASE 3 'Sort by where minted.
FOR i = 1 TO cntr 'Do not include the index string (coins$(0))
FOR k = 1 TO (cntr - 1)
IF (MID$(coins$(k),12,17)) > (MID$(coins$(k + 1),12,17)) THEN
hold$ = coins$(k) 'These three lines
coins$(k) = coins$(k + 1) 'swap strings if value
coins$(k + 1) = hold$ 'is out of order.
END IF
NEXT k
NEXT i
GOTO [display]
CASE 4 'Sort by remarks.
FOR i = 1 TO cntr 'Do not include the index string (coins$(0))
FOR k = 1 TO (cntr - 1)
IF (MID$(coins$(k), 45,19)) > (MID$(coins$(k + 1), 45,19)) THEN
hold$ = coins$(k) 'These three lines
coins$(k) = coins$(k + 1) 'swap strings if value
coins$(k + 1) = hold$ 'is out of order.
END IF
NEXT k
NEXT i
GOTO [display]
CASE ELSE
PRINT
PRINT "THAT DOES NOT COMPUTE! Please try again."
END SELECT
TIMER 2000, [choose]
WAIT
[display]
CLS
'Print a header for the output.
PRINT "VAL."; TAB(7); "DATE"; TAB(12); "MINT";TAB(30); _
"QUALITY";TAB(40);"LOC.";TAB(45);"REMARKS"
FOR p=1 to cntr 'Don't print the index string.
PRINT coins$(p)
NEXT p
PRINT
PRINT cntr; " records were printed."
INPUT "Run another sort (Y/N)? "; response$
IF response$ = "Y" OR response$ = "y" then [choose]
CLS 'Then end.
END
Now that you have the data file, let me explain that first line, the mysterious index string. This trick only works if you are using a monospaced font such as Courier or Courier New. The purpose of the index string is to provide a visual reference to the location of each data field contained in the string. Your computer has no need for a visual reference (no eyes!) and data field locations will be the same regardless of the font used. In this data file, the data fields within each string are organized like this:
There is no need to limit our string to 60 or 70 characters for visual display; it's merely a convenient choice. A string can be as many as 2MB long, but would be difficult to display on the average monitor without line breaks. When juggling strings, your sorting algorithm doesn't care whether it's juggling thimbles or six-paks.
The Six-Pak data string is easy to read and process. It is also easy for anyone to add, edit, or delete records, simply by opening coins.xyz with Word Pad, making changes being careful to keep all columns aligned, and saving the changes. For added convenience, the programmer should consider writing a GUI for editing, which can insure all information is correct when the user makes changes and evaluating user input for each data field with the LEN$() function prior to adding to the data file.
Even so, our so-simple Six-Pak data string has a major drawback. Look at all those blank spaces used solely to keep the data in the correct place within the string. Every data string in our file has 59 characters to allow for assorted values. There are 403 spaces included with only 20 records. All those spaces will eventually be loaded into one or more arrays for processing. This might be acceptable for a modest size coin shop, but it would never work for a finance company with a million or more accounts, or a parts store with a huge inventory. There are ways to deal with this problem, but we'll go into that another time.
The code is very straightforward and may be modified to suit your needs.
The 'bubble sort' was used throughout this application because I did not create a multi-dimensioned array and could not key on separate values within the string. Any of the popular sorting algorithms could be substituted. While simulating a coin collection of 1,000 records for this application, 27 to 29 seconds were required for various runs using the bubble sort. Next time I'll re-define my data file and use a multi-dimensioned array which can be sorted using the native SORT command in LB.
[Editors note: I have included Bill's data file and program in the newsletter archive. You must download the newsletter to get the associated files.]